<!DOCTYPE html>
<html><head>
	<meta charset="UTF-8">
	<meta http-equiv="x-UA-Compatible" content="ie=Edge">
   	<meta name="viewport" content="width=device-width, initial-scale=1">
   	<title>Crafty - Collision</title>
	<link type="text/css" rel="stylesheet" href="../craftyjs-site.css">
	<link rel="shortcut icon" href="../favicon.ico">
	
</head>
<body>
	<div id="header-background"> </div>
	<div id="page-frame">
	<div id="header">
		<nav class="navbar">
  			<div class="container-fluid">
  				<div class="navbar-header">
  					<a class="navbar-brand" href="../"> <img class="logo" src="../images/text-logo.png"> </a>
  					
				</div>
				<div class="collapse navbar-collapse navbar-right" id="bs-example-navbar-collapse-1">
					<ul class="nav navbar-nav">
						<li><a href="../">首页</a></li>
						<li><a href="../getting-started/">快速开始</a></li>
						<li><a href="../documentation/">文档</a></li>
						<li><a href="../api/">API</a></li>
						<li><a href="../components/">组件</a></li>
						<li class="emph"><a href="../#install">下载</a></li>
					</ul>
				</div>
			</div>
		</nav>
	</div>


	<div id="main">
		<div id="content" class="container">
			



<div id="docs"><div class="toc-holder" id="doc-nav"><ul id="doc-level-one"><li><a href="events.html">事件列表</a></li><li class="category">核心<ul class="category-list"><li><a href="Crafty.html">Crafty</a></li><li><a href="Crafty Core.html">Crafty Core</a></li><li><a href="Crafty-bind.html">Crafty.bind()</a></li><li><a href="Crafty-c.html">Crafty.c()</a></li><li><a href="Crafty-clone.html">Crafty.clone()</a></li><li><a href="Crafty-defineField.html">Crafty.defineField()</a></li><li><a href="Crafty-e.html">Crafty.e()</a></li><li><a href="Crafty-extend.html">Crafty.extend()</a></li><li><a href="Crafty-frame.html">Crafty.frame()</a></li><li><a href="Crafty-getVersion.html">Crafty.getVersion()</a></li><li><a href="Crafty-init.html">Crafty.init()</a></li><li><a href="Crafty-isPaused.html">Crafty.isPaused()</a></li><li><a href="Crafty-one.html">Crafty.one()</a></li><li><a href="Crafty-pause.html">Crafty.pause()</a></li><li><a href="Crafty-s.html">Crafty.s()</a></li><li><a href="Crafty-settings.html">Crafty.settings</a></li><li><a href="Crafty-stage.html">Crafty.stage</a></li><li><a href="Crafty-stop.html">Crafty.stop()</a></li><li><a href="Crafty-support.html">Crafty.support</a></li><li><a href="Crafty-trigger.html">Crafty.trigger()</a></li><li><a href="Crafty-unbind.html">Crafty.unbind()</a></li><li><a href="Crafty-uniqueBind.html">Crafty.uniqueBind()</a></li></ul></li><li class="category">2D<ul class="category-list"><li><a href="2D.html">2D</a></li><li><a href="AngularMotion.html">AngularMotion</a></li><li><a href="Collision.html">Collision</a></li><li><a href="Crafty-circle.html">Crafty.circle</a></li><li><a href="Crafty-diamondIso.html">Crafty.diamondIso</a></li><li><a href="Crafty-HashMap.html">Crafty.HashMap</a></li><li><a href="Crafty-isometric.html">Crafty.isometric</a></li><li><a href="Crafty-map.html">Crafty.map</a></li><li><a href="Crafty-math-Matrix2D.html">Crafty.math.Matrix2D</a></li><li><a href="Crafty-math-Vector2D.html">Crafty.math.Vector2D</a></li><li><a href="Crafty-polygon.html">Crafty.polygon</a></li><li><a href="Crafty-raycast.html">Crafty.raycast()</a></li><li><a href="Crafty-rectManager.html">Crafty.rectManager</a></li><li><a href="Gravity.html">Gravity</a></li><li><a href="GroundAttacher.html">GroundAttacher</a></li><li><a href="Motion.html">Motion</a></li><li><a href="Supportable.html">Supportable</a></li></ul></li><li class="category">动画<ul class="category-list"><li><a href="Crafty-easing.html">Crafty.easing</a></li><li><a href="SpriteAnimation.html">SpriteAnimation</a></li><li><a href="Tween.html">Tween</a></li></ul></li><li class="category">资源<ul class="category-list"><li><a href="Crafty-asset.html">Crafty.asset()</a></li><li><a href="Crafty-assets.html">Crafty.assets</a></li><li><a href="Crafty-imageWhitelist.html">Crafty.imageWhitelist()</a></li><li><a href="Crafty-load.html">Crafty.load()</a></li><li><a href="Crafty-paths.html">Crafty.paths()</a></li><li><a href="Crafty-removeAssets.html">Crafty.removeAssets()</a></li></ul></li><li class="category">音频<ul class="category-list"><li><a href="Crafty-audio.html">Crafty.audio</a></li></ul></li><li class="category">控制器<ul class="category-list"><li><a href="Controllable.html">Controllable</a></li><li><a href="Controls.html">Controls</a></li><li><a href="Draggable.html">Draggable</a></li><li><a href="Fourway.html">Fourway</a></li><li><a href="Jumper.html">Jumper</a></li><li><a href="Multiway.html">Multiway</a></li><li><a href="Twoway.html">Twoway</a></li></ul></li><li class="category">调试<ul class="category-list"><li><a href="Crafty-error.html">Crafty.error()</a></li><li><a href="Crafty-log.html">Crafty.log()</a></li><li><a href="DebugCanvas.html">DebugCanvas</a></li><li><a href="DebugPolygon.html">DebugPolygon</a></li><li><a href="DebugRectangle.html">DebugRectangle</a></li><li><a href="SolidAreaMap.html">SolidAreaMap</a></li><li><a href="SolidHitBox.html">SolidHitBox</a></li><li><a href="VisibleMBR.html">VisibleMBR</a></li><li><a href="WiredAreaMap.html">WiredAreaMap</a></li><li><a href="WiredHitBox.html">WiredHitBox</a></li></ul></li><li class="category">事件<ul class="category-list"><li><a href="Crafty-addEvent.html">Crafty.addEvent()</a></li><li><a href="Crafty-bind.html">Crafty.bind()</a></li><li><a href="Crafty-one.html">Crafty.one()</a></li><li><a href="Crafty-removeEvent.html">Crafty.removeEvent()</a></li><li><a href="Crafty-trigger.html">Crafty.trigger()</a></li><li><a href="Crafty-unbind.html">Crafty.unbind()</a></li><li><a href="Crafty-uniqueBind.html">Crafty.uniqueBind()</a></li></ul></li><li class="category">游戏循环<ul class="category-list"><li><a href="Crafty-timer.html">Crafty.timer</a></li></ul></li><li class="category">图形<ul class="category-list"><li><a href="Canvas.html">Canvas</a></li><li><a href="CanvasLayer.html">CanvasLayer</a></li><li><a href="Color.html">Color</a></li><li><a href="Crafty-assignColor.html">Crafty.assignColor()</a></li><li><a href="Crafty-background.html">Crafty.background()</a></li><li><a href="Crafty-createLayer.html">Crafty.createLayer()</a></li><li><a href="Crafty-defaultShader.html">Crafty.defaultShader()</a></li><li><a href="Crafty-domHelper.html">Crafty.domHelper</a></li><li><a href="Crafty-pixelart.html">Crafty.pixelart()</a></li><li><a href="Crafty-sprite.html">Crafty.sprite()</a></li><li><a href="Crafty-WebGLShader.html">Crafty.WebGLShader()</a></li><li><a href="DOM.html">DOM</a></li><li><a href="DomLayer.html">DomLayer</a></li><li><a href="HTML.html">HTML</a></li><li><a href="Image.html">Image</a></li><li><a href="Particles.html">Particles</a></li><li><a href="Renderable.html">Renderable</a></li><li><a href="Sprite.html">Sprite</a></li><li><a href="Text.html">Text</a></li><li><a href="WebGL.html">WebGL</a></li><li><a href="WebGLLayer.html">WebGLLayer</a></li></ul></li><li class="category">输入<ul class="category-list"><li><a href="AreaMap.html">AreaMap</a></li><li><a href="Button.html">Button</a></li><li><a href="Crafty-findPointerEventTargetByComponent.html">Crafty.findPointerEventTargetByComponent()</a></li><li><a href="Crafty-keyboardDispatch.html">Crafty.keyboardDispatch()</a></li><li><a href="Crafty-keydown.html">Crafty.keydown</a></li><li><a href="Crafty-keys.html">Crafty.keys</a></li><li><a href="Crafty-lastEvent.html">Crafty.lastEvent</a></li><li><a href="Crafty-mouseButtons.html">Crafty.mouseButtons</a></li><li><a href="Crafty-mouseDispatch.html">Crafty.mouseDispatch()</a></li><li><a href="Crafty-mouseWheelDispatch.html">Crafty.mouseWheelDispatch()</a></li><li><a href="Crafty-multitouch.html">Crafty.multitouch()</a></li><li><a href="Crafty-selected.html">Crafty.selected</a></li><li><a href="Crafty-touchDispatch.html">Crafty.touchDispatch()</a></li><li><a href="Keyboard.html">Keyboard</a></li><li><a href="Mouse.html">Mouse</a></li><li><a href="MouseDrag.html">MouseDrag</a></li><li><a href="Touch.html">Touch</a></li></ul></li><li class="category">杂项<ul class="category-list"><li><a href="Crafty-addEvent.html">Crafty.addEvent()</a></li><li><a href="Crafty-device.html">Crafty.device</a></li><li><a href="Crafty-removeEvent.html">Crafty.removeEvent()</a></li><li><a href="Crafty-support.html">Crafty.support</a></li></ul></li><li class="category">模型<ul class="category-list"><li><a href="Model.html">Model</a></li></ul></li><li class="category">场景<ul class="category-list"><li><a href="Crafty-scene.html">Crafty.scene()</a></li></ul></li><li class="category">舞台<ul class="category-list"><li><a href="Crafty-background.html">Crafty.background()</a></li><li><a href="Crafty-scene.html">Crafty.scene()</a></li><li><a href="Crafty-viewport.html">Crafty.viewport</a></li></ul></li><li class="category">工具<ul class="category-list"><li><a href="Crafty-math.html">Crafty.math</a></li><li><a href="Delay.html">Delay</a></li><li><a href="Storage.html">Storage</a></li></ul></li></ul></div><div id="doc-content" class="doc-page-holder"><div class="doc-page"><h1>Collision <span class="page-badge">组件</span></h1><div class="doclet" id="Collision"><div class="triggered-events"><h4>事件</h4><div class="trigger-list"><dl class="trigger"><dt>HitOn <span>[<span class="trigger-data"><span class="obj-name">Data</span><span class="obj-props"> = { hitData }</span></span>]</span></dt><dd>当碰撞发生时触发。除非此类型的碰撞停止，否则将不会再次触发，否则将再次请求事件(使用`resetHitChecks(component)`)。</dd></dl><dl class="trigger"><dt>HitOff <span>[<span class="trigger-data"><span class="obj-name">componentName</span><span class="obj-props"> = {String}</span></span>]</span></dt><dd>当与特定组件类型的碰撞停止时触发</dd></dl></div></div><span class="markdown"><p>用于检测任何两个凸多边形碰撞的组件。</p>
<p>如果对多个组件进行了碰撞检查，并且与多个类型的碰撞发生了模拟，那么每次碰撞都会导致一个单独的事件发生。</p>
</span><div class="note"><span class="markdown"><p><strong>注意:</strong>从事件接收到的所有数据只在事件的回调期间有效。如果你想保存数据，那就复制一份吧。</p>
</span></div><span class="markdown"><p>对于碰撞事件数据(上边的hitData)的描述，请参考文档
<code>.hit()</code>.</p>
</span><div><h4>参见</h4><ul class="see-also-list"><li><a href="2D.html">2D</a></li></ul></div></div><div><h4>方法</h4><ul class="page-toc"><li><a href="#-cbr">.cbr()</a></li><li><a href="#-checkHits">.checkHits()</a></li><li><a href="#-collision">.collision()</a></li><li><a href="#-hit">.hit()</a></li><li><a href="#-ignoreHits">.ignoreHits()</a></li><li><a href="#-onHit">.onHit()</a></li><li><a href="#-resetHitChecks">.resetHitChecks()</a></li></ul><div class="doclet" id="-cbr"><a href="#doc-nav" class="doc-top">Back to top</a><h2 class="doclet-header">.cbr()</h2><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">Object </span><span class="sig-noun">.cbr</span></span><span class="sig-seperator">([</span><span class="sig-phrase"><span class="sig-qualifier">Object </span><span class="sig-noun">cbr</span></span><span class="sig-seperator">])</span></code><dl class="parameter"><dt> cbr </dt><dd><span class="markdown"><p>用作输出的对象</p>
</span></dd></dl><dl class="parameter returns"><dt class="returns"> [Returns] </dt><dd><span class="markdown"><p>一个带有<code>_x</code>, <code>_y</code>, <code>_w</code>, 和 <code>_h</code>属性的对象;如果一个对象被传入，它将被重用而不是创建一个新对象。</p>
</span></dd></dl></div><span class="markdown"><p>返回一个包含该实体的碰撞边界矩形的副本的对象。碰撞边界矩形包含了实体的自定义碰撞和它的最小边界矩形。如果定制的碰撞盒子不位于实体外部，它将返回实体的最小边界矩形 (<code>.mbr()</code>)。</p>
</span><div class="note"><span class="markdown"><p><strong>注意:</strong> 键有一个下划线前缀。这是由于x、y、w、h属性是setter和getter，用下划线(_x, _y, _w, _h)来包装底层属性。</p>
</span></div><div><h4>参见</h4><ul class="see-also-list"><li><a href="2D.html#-mbr">2D.mbr</a></li></ul></div></div><div class="doclet" id="-checkHits"><a href="#doc-nav" class="doc-top">Back to top</a><h2 class="doclet-header">.checkHits()</h2><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">this </span><span class="sig-noun">.checkHits</span></span><span class="sig-seperator">(</span><span class="sig-phrase"><span class="sig-qualifier">String </span><span class="sig-noun">componentList</span></span><span class="sig-seperator">)</span></code><dl class="parameter"><dt> componentList </dt><dd><span class="markdown"><p>一个逗号分隔的组件列表，以检查是否与之发生碰撞。</p>
</span></dd></dl></div><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">this </span><span class="sig-noun">.checkHits</span></span><span class="sig-seperator">(</span><span class="sig-phrase"><span class="sig-qualifier">String </span><span class="sig-noun">component1</span></span><span class="sig-seperator">[,</span><span class="sig-phrase"><span class="sig-qualifier"> </span><span class="sig-noun">..</span></span><span class="sig-seperator">,</span><span class="sig-phrase"><span class="sig-qualifier"> </span><span class="sig-qualifier">String </span><span class="sig-noun">componentN</span></span><span class="sig-seperator">])</span></code><dl class="parameter"><dt> component# </dt><dd><span class="markdown"><p>用来检查碰撞的组件。</p>
</span></dd></dl></div><span class="markdown"><p>对在调用该方法时指定至少一个组件的所有实体执行碰撞检查。如果发生碰撞，将为包含该方法的实体触发一个包含碰撞信息的“HitOn”事件。请参阅<code>.hit()</code>的文档，用于描述事件中包含的碰撞数据。当报告的碰撞结束时，将会触发相应的“HitOff”事件。</p>
<p>对于相同的组件类型，多次调用该方法不会导致多余的碰撞检查。</p>
<p>如果您想要更细粒度的控制，请考虑使用<code>.hit()</code>，甚至是<code>Crafty.map.search()</code>。</p>
</span><div class="note"><span class="markdown"><p><strong>注意:</strong>在输入每个新帧时执行碰撞检查(使用<em>EnterFrame</em>事件)。在执行完检查之后，对象完全有可能在这一帧中移动(即使更多的是<em>EnterFrame</em>的结果，因为处理程序没有特定的顺序运行)。在这种情况下，除非在下一帧中执行下一个检查，才会触发命中事件。</p>
</span></div><div class="example"><h4>例子</h4><span class="markdown"><pre><code class="hljs ">Crafty.e(<span class="hljs-string">"2D, Collision"</span>)
    .checkHits(<span class="hljs-string">'Solid'</span>) <span class="hljs-comment">// check for collisions with entities that have the Solid component in each frame</span>
    .bind(<span class="hljs-string">"HitOn"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">hitData</span>) </span>{
        Crafty.log(<span class="hljs-string">"Collision with Solid entity occurred for the first time."</span>);
    })
    .bind(<span class="hljs-string">"HitOff"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">comp</span>) </span>{
        Crafty.log(<span class="hljs-string">"Collision with Solid entity ended."</span>);
    });</code></pre></span></div><div><h4>参见</h4><ul class="see-also-list"><li><a href="#-hit">.hit</a></li><li><a href="Crafty-map.html#Crafty-map-search">Crafty.map.search</a></li></ul></div></div><div class="doclet" id="-collision"><a href="#doc-nav" class="doc-top">Back to top</a><h2 class="doclet-header">.collision()</h2><div class="triggered-events"><h4>事件</h4><div class="trigger-list"><dl class="trigger"><dt>NewHitbox <span>[<span class="trigger-data"><span class="obj-name">Data</span><span class="obj-props"> = {Crafty.polygon}</span></span>]</span></dt><dd>当一个新的碰撞盒子被分配时</dd></dl></div></div><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">this </span><span class="sig-noun">.collision</span></span><span class="sig-seperator">([</span><span class="sig-phrase"><span class="sig-qualifier">Crafty.polygon </span><span class="sig-noun">polygon</span></span><span class="sig-seperator">])</span></code><dl class="parameter"><dt> polygon </dt><dd><span class="markdown"><p>可选对象 Crafty.polygon 将作为碰撞区域</p>
</span></dd></dl></div><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">this </span><span class="sig-noun">.collision</span></span><span class="sig-seperator">([</span><span class="sig-phrase"><span class="sig-qualifier">Array </span><span class="sig-noun">coordinatePairs</span></span><span class="sig-seperator">])</span></code><dl class="parameter"><dt> coordinatePairs </dt><dd><span class="markdown"><p>可选的x,y坐标对数组用来生成一个碰撞区域的多边形。</p>
</span></dd></dl></div><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">this </span><span class="sig-noun">.collision</span></span><span class="sig-seperator">([</span><span class="sig-phrase"><span class="sig-noun">x1</span></span><span class="sig-seperator">,</span><span class="sig-phrase"><span class="sig-qualifier"> </span><span class="sig-noun">y1</span></span><span class="sig-seperator">,</span><span class="sig-phrase"><span class="sig-noun">..</span></span><span class="sig-seperator">,</span><span class="sig-phrase"><span class="sig-qualifier"> </span><span class="sig-noun">xN</span></span><span class="sig-seperator">,</span><span class="sig-phrase"><span class="sig-qualifier"> </span><span class="sig-noun">yN</span></span><span class="sig-seperator">])</span></code><dl class="parameter"><dt> point# </dt><dd><span class="markdown"><p>可选的x,y坐标对列表用来生成一个碰撞区域的多边形。</p>
</span></dd></dl></div><span class="markdown"><p>构造函数接受一个多边形，一个点的数组或一个点的列表作为击中区域，点相对于物体在未旋转状态下的位置。</p>
<p>碰撞区域必须是凸形，而不是凹面，以使碰撞检测工作正常。</p>
<p>如果没有传递参数，将使用该实体的x、y、w、h属性，并且当该实体存在时，碰撞盒子将会调整大小。</p>
<p>如果在实体的边界之外设置了一个碰撞盒子，那么将会有一个小的性能损失，因为它被单独跟踪。</p>
<p>为了让定制的碰撞盒子有任何效果，您必须将<code>Collision</code>添加到该实体需要使用这个定制的碰撞盒子的所有其他实体中。相反，碰撞将使用默认的碰撞盒子来解决。参见<code>.hit()</code><code>MBR</code>代表默认的碰撞盒子，<code>SAT</code>表示自定义的碰撞盒子。</p>
</span><div class="example"><h4>例子</h4><span class="markdown"><pre><code class="hljs ">Crafty.e(<span class="hljs-string">"2D, Collision"</span>).collision(
    <span class="hljs-keyword">new</span> Crafty.polygon([<span class="hljs-number">50</span>, <span class="hljs-number">0</span>,  <span class="hljs-number">100</span>, <span class="hljs-number">100</span>,  <span class="hljs-number">0</span>, <span class="hljs-number">100</span>])
);

Crafty.e(<span class="hljs-string">"2D, Collision"</span>).collision([<span class="hljs-number">50</span>, <span class="hljs-number">0</span>,  <span class="hljs-number">100</span>, <span class="hljs-number">100</span>,  <span class="hljs-number">0</span>, <span class="hljs-number">100</span>]);

Crafty.e(<span class="hljs-string">"2D, Collision"</span>).collision(<span class="hljs-number">50</span>, <span class="hljs-number">0</span>,  <span class="hljs-number">100</span>, <span class="hljs-number">100</span>,  <span class="hljs-number">0</span>, <span class="hljs-number">100</span>);</code></pre></span></div><div><h4>参见</h4><ul class="see-also-list"><li><a href="Crafty-polygon.html">Crafty.polygon</a></li></ul></div></div><div class="doclet" id="-hit"><a href="#doc-nav" class="doc-top">Back to top</a><h2 class="doclet-header">.hit()</h2><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">Array </span><span class="sig-noun">.hit</span></span><span class="sig-seperator">(</span><span class="sig-phrase"><span class="sig-qualifier">String </span><span class="sig-noun">component</span></span><span class="sig-seperator">)</span></code><dl class="parameter"><dt> component </dt><dd><span class="markdown"><p>检查与有此组件的实体之间的碰撞。</p>
</span></dd></dl><dl class="parameter returns"><dt class="returns"> [Returns] </dt><dd><span class="markdown"><p>如果没有碰撞就会返回 <code>null</code>。如果检测到冲突，则返回一个碰撞数据对象数组(见下文)。</p>
</span></dd></dl></div><span class="markdown"><p>与具有指定组件的实体进行碰撞测试。如果检测到碰撞，将在该方法返回的阵列中显示关于碰撞的数据。如果没有发生冲突，则该方法返回<code>null</code>。</p>
<p>下面是碰撞的描述数据对象,这种方法可能返回:返回的碰撞数据是一个用于碰撞类型的数组或对象，如果使用的类型是SAT(一个用于碰撞盒子的多边形)，对象是物体碰撞的重叠量。</p>
<pre><code class="hljs ">[{
   <span class="hljs-attr">obj</span>: [entity],
   <span class="hljs-attr">type</span>: [<span class="hljs-string">"MBR"</span> or <span class="hljs-string">"SAT"</span>],
   <span class="hljs-attr">overlap</span>: [number]
}]</code></pre><ul>
<li><strong>obj:</strong>碰撞发生的实体。</li>
<li><strong>type:</strong> 碰撞检测方法。下边之一:<ul>
<li><em>MBR:</em>标准轴对准矩形交点 (<code>.intersect</code> 在 2D 组件中).</li>
<li><em>SAT:</em> 两个凸多边形之间的碰撞。当两个碰撞的实体都有<code>Collision</code>组件应用到它们的时候。</li>
</ul>
</li>
<li><strong>overlap:</strong>如果使用了SAT碰撞，这将表示碰撞实体之间的重叠百分比。</li>
</ul>
<p>请记住，两个实体都需要有<code>Collision</code>组件，如果您想要检查它们之间的<code>SAT</code>(定制的碰撞盒子)碰撞。</p>
<p>如果您想要更细粒度的控制，请考虑使用<code>Crafty.map.search()</code>.</p>
</span><div class="example"><h4>例子</h4><span class="markdown"><p>解决与移动实体(玩家)的静态碰撞(墙)的碰撞。</p>
<pre><code class="hljs ">Crafty.e(<span class="hljs-string">"2D, Fourway, Collision, player"</span>)
      .attr({<span class="hljs-attr">x</span>: <span class="hljs-number">32</span>, <span class="hljs-attr">y</span>: <span class="hljs-number">32</span>, <span class="hljs-attr">w</span>: <span class="hljs-number">32</span>, <span class="hljs-attr">h</span>: <span class="hljs-number">32</span>})
      .collision([<span class="hljs-number">0</span>, <span class="hljs-number">16</span>, <span class="hljs-number">16</span>, <span class="hljs-number">0</span>, <span class="hljs-number">32</span>, <span class="hljs-number">16</span>, <span class="hljs-number">16</span>, <span class="hljs-number">32</span>])
      .fourway()
      .bind(<span class="hljs-string">'Moved'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">evt</span>) </span>{ <span class="hljs-comment">// after player moved</span>
        <span class="hljs-keyword">var</span> hitDatas, hitData;
        <span class="hljs-keyword">if</span> ((hitDatas = <span class="hljs-keyword">this</span>.hit(<span class="hljs-string">'wall'</span>))) { <span class="hljs-comment">// check for collision with walls</span>
          hitData = hitDatas[<span class="hljs-number">0</span>]; <span class="hljs-comment">// resolving collision for just one collider</span>
          <span class="hljs-keyword">if</span> (hitData.type === <span class="hljs-string">'SAT'</span>) { <span class="hljs-comment">// SAT, advanced collision resolution</span>
            <span class="hljs-comment">// move player back by amount of overlap</span>
            <span class="hljs-keyword">this</span>.x -= hitData.overlap * hitData.normal.x;
            <span class="hljs-keyword">this</span>.y -= hitData.overlap * hitData.normal.y;
          } <span class="hljs-keyword">else</span> { <span class="hljs-comment">// MBR, simple collision resolution</span>
            <span class="hljs-comment">// move player to position before he moved (on respective axis)</span>
            <span class="hljs-keyword">this</span>[evt.axis] = evt.oldValue;
          }
        }
      });</code></pre></span></div><div><h4>参见</h4><ul class="see-also-list"><li><a href="Crafty-map.html#Crafty-map-search">Crafty.map.search</a></li></ul></div></div><div class="doclet" id="-ignoreHits"><a href="#doc-nav" class="doc-top">Back to top</a><h2 class="doclet-header">.ignoreHits()</h2><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">this </span><span class="sig-noun">.ignoreHits</span></span><span class="sig-seperator">()</span></code></div><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">this </span><span class="sig-noun">.ignoreHits</span></span><span class="sig-seperator">(</span><span class="sig-phrase"><span class="sig-qualifier">String </span><span class="sig-noun">componentList</span></span><span class="sig-seperator">)</span></code><dl class="parameter"><dt> componentList </dt><dd><span class="markdown"><p>逗号分隔的组件列表，以停止检查与之碰撞。</p>
</span></dd></dl></div><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">this </span><span class="sig-noun">.ignoreHits</span></span><span class="sig-seperator">(</span><span class="sig-phrase"><span class="sig-qualifier">String </span><span class="sig-noun">component1</span></span><span class="sig-seperator">[,</span><span class="sig-phrase"><span class="sig-qualifier"> </span><span class="sig-noun">..</span></span><span class="sig-seperator">,</span><span class="sig-phrase"><span class="sig-qualifier"> </span><span class="sig-qualifier">String </span><span class="sig-noun">componentN</span></span><span class="sig-seperator">])</span></code><dl class="parameter"><dt> component# </dt><dd><span class="markdown"><p>一个停止检查与碰撞的组件。</p>
</span></dd></dl></div><span class="markdown"><p>停止检查与所有或某些组件的碰撞。如果没有参数调用，该方法将导致对实体的所有碰撞检查停止。要禁用与特定组件的碰撞检查，请将组件指定为逗号分隔的字符串或作为一组参数。</p>
<p>使用组件名的方式调用此方法时，在没有碰撞检测的情况下没有效果。</p>
</span><div class="example"><h4>例子</h4><span class="markdown"><pre><code class="hljs ">Crafty.e(<span class="hljs-string">"2D, Collision"</span>)
    .checkHits(<span class="hljs-string">'Solid'</span>)
    ...
    .ignoreHits(<span class="hljs-string">'Solid'</span>); <span class="hljs-comment">// stop checking for collisions with entities that have the Solid component</span></code></pre></span></div></div><div class="doclet" id="-onHit"><a href="#doc-nav" class="doc-top">Back to top</a><h2 class="doclet-header">.onHit()</h2><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">this </span><span class="sig-noun">.onHit</span></span><span class="sig-seperator">(</span><span class="sig-phrase"><span class="sig-qualifier">String </span><span class="sig-noun">component</span></span><span class="sig-seperator">,</span><span class="sig-phrase"><span class="sig-qualifier"> </span><span class="sig-qualifier">Function </span><span class="sig-noun">callbackOn</span></span><span class="sig-seperator">[,</span><span class="sig-phrase"><span class="sig-qualifier"> </span><span class="sig-qualifier">Function </span><span class="sig-noun">callbackOff</span></span><span class="sig-seperator">])</span></code><dl class="parameter"><dt> component </dt><dd><span class="markdown"><p>用于检查碰撞的组件。</p>
</span></dd></dl><dl class="parameter"><dt> callbackOn </dt><dd><span class="markdown"><p>回调方法在与组件发生碰撞时执行。传递的第一个参数将是碰撞检查的结果，其格式与<code>hit()</code>记录的格式相同。第二个参数传递的是一个布尔值，指示是否第一次发生与组件的冲突。</p>
</span></dd></dl><dl class="parameter"><dt> callbackOff </dt><dd><span class="markdown"><p>一旦冲突停止，回调方法就会执行一次。</p>
</span></dd></dl></div><span class="markdown"><p>Creates an EnterFrame event calling <code>.hit()</code> each frame.  When a collision is detected the <code>callbackOn</code> will be invoked.</p>
<p>Note that the <code>callbackOn</code> will be invoked every frame the collision is active, not just the first time the collision occurs.
Use the second argument passed to <code>callbackOn</code> to differentiate that, which will be <code>true</code> if it's the first time the collision occurs.</p>
<p>If you want more fine-grained control consider using <code>.checkHits()</code>, <code>.hit()</code> or even <code>Crafty.map.search()</code>.</p>
</span><div class="example"><h4>例子</h4><span class="markdown"><p>对玩家和子弹之间的碰撞做出反应。</p>
<pre><code class="hljs ">Crafty.e(<span class="hljs-string">"2D, Collision, player"</span>)
      .attr({ <span class="hljs-attr">health</span>: <span class="hljs-number">100</span> })
      .onHit(<span class="hljs-string">'bullet'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">hitDatas</span>) </span>{ <span class="hljs-comment">// on collision with bullets</span>
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>, l = hitDatas.length; i &lt; l; ++i) { <span class="hljs-comment">// for each bullet hit</span>
          hitDatas[i].obj.destroy(); <span class="hljs-comment">// destroy the bullet</span>
          <span class="hljs-keyword">this</span>.health -= <span class="hljs-number">25</span>; <span class="hljs-comment">// player looses health</span>
          <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.health &lt;= <span class="hljs-number">0</span>) <span class="hljs-comment">// once player's health depletes</span>
            <span class="hljs-keyword">this</span>.destroy(); <span class="hljs-comment">// player dies</span>
        }
      });</code></pre></span></div><div><h4>参见</h4><ul class="see-also-list"><li><a href="#-checkHits">.checkHits</a></li><li><a href="#-hit">.hit</a></li><li><a href="Crafty-map.html#Crafty-map-search">Crafty.map.search</a></li></ul></div></div><div class="doclet" id="-resetHitChecks"><a href="#doc-nav" class="doc-top">Back to top</a><h2 class="doclet-header">.resetHitChecks()</h2><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">this </span><span class="sig-noun">.resetHitChecks</span></span><span class="sig-seperator">()</span></code></div><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">this </span><span class="sig-noun">.resetHitChecks</span></span><span class="sig-seperator">(</span><span class="sig-phrase"><span class="sig-qualifier">String </span><span class="sig-noun">componentList</span></span><span class="sig-seperator">)</span></code><dl class="parameter"><dt> componentList </dt><dd><span class="markdown"><p>一个逗号分隔的组件列表，以重新检查与之碰撞。</p>
</span></dd></dl></div><div class="crafty-method"><code class="signature"><span class="sig-phrase"><span class="sig-qualifier">public </span><span class="sig-qualifier">this </span><span class="sig-noun">.resetHitChecks</span></span><span class="sig-seperator">(</span><span class="sig-phrase"><span class="sig-qualifier">String </span><span class="sig-noun">component1</span></span><span class="sig-seperator">[,</span><span class="sig-phrase"><span class="sig-qualifier"> </span><span class="sig-noun">..</span></span><span class="sig-seperator">,</span><span class="sig-phrase"><span class="sig-qualifier"> </span><span class="sig-qualifier">String </span><span class="sig-noun">componentN</span></span><span class="sig-seperator">])</span></code><dl class="parameter"><dt> component# </dt><dd><span class="markdown"><p>用于重新检查与之碰撞的组件。</p>
</span></dd></dl></div><span class="markdown"><p>在已经发生的碰撞中，会引起碰撞事件(通常情况下，在碰撞停止并再次发生之前，一个额外的事件不会发生)。如果没有参数调用，该方法将导致对实体的所有碰撞检查再次触发事件。要重新检查与特定组件的碰撞，请将组件指定为逗号分隔的字符串或作为一组参数。</p>
<p>使用组件名的方式调用此方法时，在没有碰撞检测的情况下没有效果。</p>
</span><div class="example"><h4>例子</h4><span class="markdown"><pre><code class="hljs "><span class="hljs-comment">// this example fires the HitOn event each frame the collision with the Solid entity is active, instead of just the first time the collision occurs.</span>
Crafty.e(<span class="hljs-string">"2D, Collision"</span>)
    .checkHits(<span class="hljs-string">'Solid'</span>)
    .bind(<span class="hljs-string">"HitOn"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">hitData</span>) </span>{
        Crafty.log(<span class="hljs-string">"Collision with Solid entity was reported in this frame again!"</span>);
        <span class="hljs-keyword">this</span>.resetHitChecks(<span class="hljs-string">'Solid'</span>); <span class="hljs-comment">// fire the HitOn event in the next frame also, if the collision is still active.</span>
    })</code></pre></span></div></div></div></div></div></div>
			
		</div>
	</div>
	<div class="clearer"></div>
	<div id="footer">
		<div id="contact">
			<a href="https://groups.google.com/forum/#!forum/craftyjs">
				<img src="../images/google.png">
				google groups</a>
			<a href="mailto:starwed@gmail.com">
				<img src="../images/email.png">
				starwed@gmail.com</a>
			<a href="https://github.com/craftyjs/Crafty">
				<img src="../images/github.png">
				github</a>
		</div>
		<ul>
			<li><a href="../">首页</a></li>
				<li><a href="../getting-started/">快速开始</a></li>
				<li><a href="../documentation/">文档</a></li>
				<li><a href="../api/">API</a></li>
				<li><a href="../components/">组件</a></li>
				<li class="emph"><a href="../#install">下载</a></li>
		</ul>
		<p>
			© Crafty 2010-2015. Crafty is distributed under the <a href="http://en.wikipedia.org/wiki/MIT_License">MIT License</a>.
		</p>
	</div>
</div>


</body></html>